package com.spr.gw.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.spr.gw.common.PageBean;
import com.spr.gw.common.ResponseCode;
import com.spr.gw.common.ResultBean;
import com.spr.gw.entity.Category;
import com.spr.gw.entity.Message;
import com.spr.gw.entity.VO.MessageVo;
import com.spr.gw.mapper.CategoryMapper;
import com.spr.gw.mapper.MessageMapper;
import com.spr.gw.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static java.util.stream.Collectors.toList;

@Service
public class MessageSerivceImpl implements MessageService {

    @Autowired
    private MessageMapper messageMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public PageBean<MessageVo> findMessages(MessageVo filterMessage) {
        //查询总页数,不必要，查询完的pageHelper对象转化之后可以获得信息
        //int count = messageMapper.selectCount(filterMessage);
        //开启pageHelper分页，下一条mybatis查询语句会使用分页
        PageHelper.startPage(filterMessage.getPageNum(), filterMessage.getPageSize());
        List<MessageVo> messages = messageMapper.selectMessageVo(filterMessage);
        // Page<MessageVo> pageHp = (Page<MessageVo>) messages;
        // PageInfo 里面的list对象是Page实例的↑↑↑,所以PageInfo更全面
        PageInfo pageHp = new PageInfo(messages); //下面这个也可以获得这个对象信息
        PageBean<MessageVo> pageData = new PageBean(pageHp.getPageNum(), pageHp.getPageSize(), (int) pageHp.getTotal());
        pageData.setResult(messages);
        return pageData;
    }

    @Override
    public ResultBean<Message> insertMessage(Message message) {
        message.setUpdateTime(new Date());
        int i;
        if(!StringUtils.isEmpty(message.getMessageId())){
             i = messageMapper.updateByPrimaryKeySelective(message);
        } else{
            message.setIsDel(1);
            message.setClick(0);
            message.setCreateTime(new Date());
            i = messageMapper.insert(message);
        }
        if(i != 0){
            return ResultBean.createBySuccess();
        }else{
            return ResultBean.createByCodeMessage(ResponseCode.ADD_ERROR.getCode(),ResponseCode.ADD_ERROR.getDesc());
        }
    }

    @Override
    public Message findOneMessage(Integer id) {
        return messageMapper.selectByPrimaryKey(id);
    }

    @Override
    public ResultBean<Message> messageDelete(Integer id) {
        int i = messageMapper.deleteByPrimaryKey(id);
        if(i == 0){
            return ResultBean.createByCodeMessage(ResponseCode.DELETE_ERROR.getCode(),ResponseCode.DELETE_ERROR.getDesc());
        }
        return ResultBean.createBySuccess();
    }

    @Override
    public PageBean<MessageVo> findMessagesByCategoryId(Integer categoryId, Integer pageNum) {
        if(pageNum == null)
            pageNum = 1;
        //开启pageHelper分页，下一条mybatis查询语句会使用分页
        PageHelper.startPage(pageNum, 10);
        List<MessageVo> list = messageMapper.selectByCategoryId(Message.builder().categoryId(categoryId).build());
        Page<MessageVo> pageHp = (Page<MessageVo>) list;
        PageBean<MessageVo> pageData = new PageBean(pageHp.getPageNum(), pageHp.getPageSize(), (int) pageHp.getTotal());
        pageData.setResult(list);
        return pageData;
    }

    @Override
    public ResultBean<List<MessageVo>> findAllMessageByType(String type) {
        //查询所有的图片类目id
        List<Category> categorys = categoryMapper.select(Category.builder().type(type).build());
        //  List<String> ids = new ArrayList<>();
        //   categorys.forEach(category -> ids.add(category.getCategoryId().toString()));
        List<String> ids = categorys.stream()
                .map(c -> c.getCategoryId().toString())
                .collect(toList());
        List<MessageVo> list = messageMapper.selectPictureMessage(ids);
        if(list.size() > 0){
            return ResultBean.createBySuccess(list);
        }else{
            return ResultBean.createByCodeMessage(ResponseCode.NO_RESULT.getCode(),ResponseCode.NO_RESULT.getDesc());
        }
    }

    @Override
    public Message findOnePageContent(Integer categoryId) {
        Message message = messageMapper.getOneMessageByCategoryId(categoryId);
        return message;
    }

    @Override
    public List<MessageVo> findAllMessageByCategoryId(Integer categoryId) {
        List<MessageVo> messages = messageMapper.selectByCategoryId(Message.builder().categoryId(categoryId).build());
        return messages;
    }

}
